package com.dongdong.android.framework.download;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.util.HashMap;

public class DownloadHandler extends Handler {

    private HashMap<String, DownloadStatusListener> mDownListenerQueue;

    public DownloadHandler(HashMap<String, DownloadStatusListener> mDownListenerQueue) {
        super(Looper.getMainLooper());
        this.mDownListenerQueue = mDownListenerQueue;
    }


    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        FileInfo info = (FileInfo) msg.obj;
        String keyUrl = info.getUrl();
        DownloadStatusListener listener = mDownListenerQueue.get(keyUrl);
        if (listener != null) {
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    listener.progress(keyUrl, info.getProgress());
                    break;
                case 2:
                    listener.pause(keyUrl);
                    break;
                case 3:
                    listener.cancle(keyUrl);
                    break;
                case 4:
                    listener.fail(keyUrl, "下载失败!");
                    break;
                case 5:
                    listener.error(keyUrl, "下载出错!");
                    break;
                case 6:
                    listener.success(info);
                    break;
                default:
                    break;
            }
        }
    }
}
